/////////////////////////////////////////////////////////////////////////////
// Name:        maintoolbar.cpp
// Purpose:     
// Author:      Brant Young
// Modified by: 
// Created:     Mon May 14 21:46:51 2007
// RCS-ID:      
// Copyright:   Copyright (c) daodict.com 2007
// Licence:     GPL 2.0
/////////////////////////////////////////////////////////////////////////////
#include <wx/wx.h>

#include "wordstoolbar.h"
#include "wordswnd.h"
#include "../../core/thememgr.h"
#include "../prefers/prefersdialog.h"
#include "../addons/addonsdialog.h"

/**
 * WordsToolBar constructors
 */
WordsToolBar::WordsToolBar(WordsWindow* p_parent) : wxToolBar(p_parent, ID_WORDS_TOOLBAR, \
                                       wxDefaultPosition, wxDefaultSize, \
                                       wxTB_FLAT|wxTB_HORIZONTAL|wxNO_BORDER |\
                                       wxTB_NODIVIDER )
{
    // this->SetBackgroundColour(wxColour(247, 245, 204));
    this->SetToolBitmapSize(wxSize(16, 16));

    _p_thememgr = &ThemeManager::get();
    _p_parent = p_parent;

    // -----------------------------------------------------------
    
    wxBitmap& bmp = _p_thememgr->get_bitmap(_T("audio-volume-16.png"));
    this->AddTool(ID_PRONOUN, _(" Spell"), bmp, wxNullBitmap, wxITEM_NORMAL, _("Pronounce the Word"), _("Pronounce the Word"));
    
    // -------------------
    this->AddSeparator();
    // -------------------
    
    bmp = _p_thememgr->get_bitmap(_T("edit-clear-16.png"));
    this->AddTool(ID_CLEAR, _T(" Clear"), bmp, wxNullBitmap, wxITEM_NORMAL, _("Clear History"), _("Go next one word"));

    bmp = _p_thememgr->get_bitmap(_T("go-previous-16.png"));
    this->AddTool(wxID_BACKWARD, _(" Previous"), bmp, wxNullBitmap, wxITEM_NORMAL, _("Previous Word"), _("Go back one word"));
    

    //bmp = _p_thememgr->get_bitmap(_T("go-previous-16.png"));
    p_history_cmbox = new wxBitmapComboBox(this, ID_HISTORY_CMBOX, _(""), \
    		wxDefaultPosition, wxSize(160,-1), 0, NULL, wxCB_READONLY);
   	bmp = _p_thememgr->get_bitmap(_T("wordslist-16.png"));
   	p_history_cmbox->Append(_T("History Word"), bmp);
   	p_history_cmbox->Select(0);
    
    this->AddControl(p_history_cmbox);
    
    bmp = _p_thememgr->get_bitmap(_T("go-next-16.png"));
    this->AddTool(wxID_FORWARD, _T(" Next"), bmp, wxNullBitmap, wxITEM_NORMAL, _("Next Word"), _("Go next one word"));
    
    // -------------------
    this->AddSeparator();
    // -------------------
    
    bmp = _p_thememgr->get_bitmap(_T("edit-find-16.png"));
    this->AddTool(ID_FIND, _(" Find"), bmp, wxNullBitmap, wxITEM_NORMAL, _("Find"), _("Find in current explanation"));

    
    this->Realize();
    EnableTool(wxID_FORWARD, false);
    EnableTool(wxID_BACKWARD, false);
    
    this->InitEvents();
}


/**
 * WordsToolBar destructor
 */

WordsToolBar::~WordsToolBar()
{
////@begin WordsToolBar destruction
////@end WordsToolBar destruction
}


void WordsToolBar::InitEvents() {
	this->Connect(ID_HISTORY_CMBOX, wxEVT_COMMAND_COMBOBOX_SELECTED, 
			(wxObjectEventFunction)&WordsToolBar::OnSelectHistroy);
	
	this->Connect(ID_CLEAR, wxEVT_COMMAND_TOOL_CLICKED, 
			(wxObjectEventFunction)&WordsToolBar::OnClearHistory);
	
	this->Connect(wxID_FORWARD, wxEVT_COMMAND_TOOL_CLICKED, 
			(wxObjectEventFunction)&WordsToolBar::OnForwardHistroy);
	
	this->Connect(wxID_BACKWARD, wxEVT_COMMAND_TOOL_CLICKED, 
			(wxObjectEventFunction)&WordsToolBar::OnBackwardHistroy);
}


void WordsToolBar::add_word_2_history(const wxString& word) {
   	wxBitmap& bmp = _p_thememgr->get_bitmap(_T("wordslist-16.png"));
   	p_history_cmbox->Insert(word, bmp, 0);
   	
   	int count = p_history_cmbox->GetCount();
   	if ( 100<count ) {
   		p_history_cmbox->Delete(count-1);
   	}

   	EnableTool(wxID_FORWARD, false);
   	
   	p_history_cmbox->Select(0);
   	if (can_backward(0)) {
   		EnableTool(wxID_BACKWARD, true);
   	}
}

void WordsToolBar::OnSelectHistroy(wxCommandEvent& event) {
	int index = p_history_cmbox->GetSelection();

	EnableTool(wxID_FORWARD, true);
	EnableTool(wxID_BACKWARD, true);
	
	if ( not can_forward(index) ) {
		EnableTool(wxID_FORWARD, false);
		EnableTool(wxID_BACKWARD, true);
	}
		
	if ( not can_backward(index) ) {
		EnableTool(wxID_FORWARD, true);
		EnableTool(wxID_BACKWARD, false);
	}
		
	_p_parent->show_word_dicts_and_default(p_history_cmbox->GetValue());
}

void WordsToolBar::OnClearHistory(wxCommandEvent& event) {
   	int count = p_history_cmbox->GetCount();
   	p_history_cmbox->Clear();
   	
   	wxBitmap& bmp = _p_thememgr->get_bitmap(_T("wordslist-16.png"));
   	p_history_cmbox->Append(_T("History Word"), bmp);
   	p_history_cmbox->Select(0);
   	
   	EnableTool(wxID_FORWARD, false);
   	EnableTool(wxID_BACKWARD, false);
}


void WordsToolBar::OnForwardHistroy(wxCommandEvent& event) {
	int index = p_history_cmbox->GetSelection() - 1;

	EnableTool(wxID_BACKWARD, true);
	
	if ( not can_forward(index) ) {
		EnableTool(wxID_FORWARD, false);
	}
	
	_p_parent->show_word_dicts_and_default(p_history_cmbox->GetString(index));
	p_history_cmbox->Select(index);
}


void WordsToolBar::OnBackwardHistroy(wxCommandEvent& event) {
	int index = p_history_cmbox->GetSelection() + 1;

	EnableTool(wxID_FORWARD, true);
	
	if ( not can_backward(index) ) {
		EnableTool(wxID_BACKWARD, false);
	}
	
	
	_p_parent->show_word_dicts_and_default(p_history_cmbox->GetString(index));
	p_history_cmbox->Select(index);
}


